<template>
	<view class="content">
		<view class="row">
			<view class="lefttou">
				<image class="tou" src="../../static/gerenzhongxin/10.png" mode=""></image>
				风险治理
			</view>
			<text class="num" v-if="userData.noCheck!=0">{{userData.noCheck}}</text>
		</view>
		<view class="row p2010">
			<view class="w50">
				一般隐患
				<text class="num" v-if="userData.lessDanger!=0">{{userData.lessDanger}}</text>
			</view>
			<view class="w50">
				重大隐患
				<text class="num" v-if="userData.highDanger!=0">{{userData.highDanger}}</text>
			</view>
		</view>
		<view class="row p2010">
			<view class="w50">
				已验收
				<text class="num" v-if="userData.hasCheck!=0">{{userData.hasCheck}}</text>
			</view>
			<view class="w50">
				未验收
				<text class="num" v-if="userData.noCheck!=0">{{userData.noCheck}}</text>
			</view>
		</view>
		<view class="bumen">
			<view class="text">部门</view>
			<view class="bumenright">
				<picker @change="YHXLChange" :value="YHXLIndex" :range="YHXL" range-key="name">
					<view class="uni-input">
						{{YHXL[YHXLIndex].name}}
						<uni-icons type="arrowdown" size="20"></uni-icons>
					</view>
				</picker>
			</view>
		</view>
		<view class="today">
			今天
		</view>
		<view class="message-list">
			<wkiwi-swipe-action @endMove="endMove" @clickItem="clickItem" :options="options" :messagesList="messagestoday"></wkiwi-swipe-action>
		</view>
		<view class="today">
			更早
		</view>
		<view class="message-list">
			<wkiwi-swipe-action @endMove="endMove" @clickItem="clickItem" :options="options" :messagesList="messagesbefor"></wkiwi-swipe-action>
			<view class="more" v-if="isShowMore" @click="fxsspsspLog">加载更多</view>
			<view class="more" v-else>已全部加载</view>
		</view>
	</view>
</template>

<script>
	import uniIcons from "@/components/uni-icons/uni-icons.vue"
	import wkiwiSwipeAction from "../../components/catList/wkiwi-swipe-action.vue";
	export default {
		components: {
			uniIcons,
			wkiwiSwipeAction
		},
		data() {
			return {
				fxsspsspLogData: [],
				userData: [],
				YHXL: [],
				YHXLIndex: 0,
				focus: false,
				isShowView: true,
				isShowMore: false,
				page: 1,
				options: [{
						text: "",
						style: {
							backgroundColor: "#C7C6CD"
						}
					},
					{
						text: "",
						style: {
							backgroundColor: "#dd524d"
						}
					}
				],
				messagestoday: [],
				messagesbefor:[],
				
			};
		},
		onLoad() {
			// #ifdef APP-PLUS
			/* 隐藏凸起图标 */
			var icon = plus.nativeObj.View.getViewById("icon");
			setTimeout(function() {
				if(icon){icon.hide();}
			}, 100);
			// #endif
			this.findStatus();
			this.fxsspsspLog();
			this.getDeptList();
			this.getNowTimeNum = this.getNowTime()
		},
		methods: {
			//日志列表
			fxsspsspLog() {
				var _this = this;
				var data = {
					deptId: _this.YHXLIndex == 0 ? '' : _this.YHXLIndex,
					is_delete: 0
				}
				
				uni.showLoading({title:'加载中'})
				uni.request({
					url: this.$API.fxsspsspLog,
					method: "POST",
					data: {
						where: JSON.stringify(data),
						page: _this.page
					},
					header: this.$API.get_head(),
					success: (res) => {
						uni.hideLoading()
						_this.fxsspsspLogData = res.data.rows;
						_this.fxsspsspLogData.forEach(function(item,index){
							item['url'] =["../../static/gerenzhongxin/listfx.png"]
							item['stick'] = false
							item['disabled'] = false
							item['type'] = 2;
							
							// 根据addtime字段判断是今天还是更早，其中addtime为空的数据直接过滤掉
							if(item.addtime){
								if(_this.getNowTimeNum > item.addtime.substring(0,10)){
									_this.messagesbefor.push(item)
								}else{
									_this.messagestoday.push(item)
								}
							}
						});
						
						// 判断数据是否加载完毕
						_this.isShowMore = _this.page <= res.data.total;
						_this.page++;
					},
					fail: () => {
						uni.hideLoading()
						uni.showToast({
							title: "加载失败",
							icon: 'none'
						})
					}
				})
			},
			//上面四个数字
			findStatus() {
				var _this = this;
				uni.request({
					url: this.$API.findStatus,
					method: "POST",
					data: {
						userid: uni.getStorageSync("userid"),
					},
					header: this.$API.get_head(),
					success: (res) => {
						_this.userData = res.data.data;
					},
					fail: () => {
						uni.showToast({
							title: "加载失败",
							icon: 'none'
						})
					}
				})
			},
			// 切换部门
			YHXLChange(e) {
				this.YHXLIndex = e.target.value;
				this.page = 1;
				this.isShowMore = false;
				this.messagestoday = [];
				this.messagesbefor = [];
				this.fxsspsspLog();
			},
			// 滑动
			endMove(data){
				let dir = data.dir;
				let sspid = data.data.sspid;
				if(dir == 'left') this.slideLeft(sspid);
				if(dir == 'right') this.slideRight(sspid);
			},
			// 获取部门列表
			getDeptList(){
				var _this = this;
				uni.getStorage({
					key: 'company',
					success: function (companyRes){
						uni.request({
							url: _this.$API.deptList,
							method: "GET",
							data: {company: companyRes.data},
							header: _this.$API.get_head(),
							success: (res) => {
								let tmpDepts = res.data.data.map(item=>{
									item.name = item.fullname
									return item
								})
								tmpDepts.unshift({id: '', name: '所有部门'});
								_this.YHXL = tmpDepts
							},
							fail: () => {
								uni.showToast({
									title: "加载失败",
									icon: 'none'
								})
							}
						})
					}
				})
			},
			// 左滑取消
			slideLeft(sspid){
				let _this = this
				uni.showModal({
					content: '您确认要取消吗？',
					success: function (res){
						if(res.confirm){
							uni.showLoading({
								title: '取消中'
							})
							uni.request({
								url: _this.$API.cancelDanger,
								method: "POST",
								data: {
									sspid,
									userid: uni.getStorageSync("userid")
								},
								header: _this.$API.get_head(),
								success: (res) => {
									uni.hideLoading()
									_this.messagestoday = _this.messagesbefor.filter(it=>it.sspid != sspid)
									_this.messagesbefor = _this.messagesbefor.filter(it=>it.sspid != sspid)
								},
								fail: () => {
									uni.hideLoading()
									uni.showToast({
										title: "加载失败",
										icon: 'none'
									})
								}
							})
						}
					}
				})
			},
			// 右滑推送
			slideRight(sspid){
				let _this = this
				uni.request({
					url: _this.$API.pushDanger,
					method: "POST",
					data: {
						sspid,
						userid: uni.getStorageSync("userid")
					},
					header: _this.$API.get_head(),
					success: (res) => {
						uni.showToast({title: "推送成功"})
					},
					fail: () => {
						uni.showToast({
							title: "加载失败",
							icon: 'none'
						})
					}
				})
			},
			// 点击处理
			clickItem(item){
				uni.navigateTo({
					url: `../organization/finishTheTask?sspid=${item.sspid}`
				})
			},
			//获取时间
			getNowTime() {
				// 加0
				function add_10(num) {
					if (num < 10) {
						num = '0' + num
					}
					return num;
				}
				var myDate = new Date();
				myDate.getYear(); //获取当前年份(2位)
				myDate.getFullYear(); //获取完整的年份(4位,1970-????)
				myDate.getMonth(); //获取当前月份(0-11,0代表1月)
				myDate.getDate(); //获取当前日(1-31)
				myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
				myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
				myDate.getHours(); //获取当前小时数(0-23)
				myDate.getMinutes(); //获取当前分钟数(0-59)
				myDate.getSeconds(); //获取当前秒数(0-59)
				myDate.getMilliseconds(); //获取当前毫秒数(0-999)
				myDate.toLocaleDateString(); //获取当前日期
				// var nowTime = myDate.getFullYear() + '-' + add_10(myDate.getMonth()) + '-' + myDate.getDate() + ' ' + add_10(myDate.getHours()) + ':' + add_10(myDate.getMinutes()) + ':' + add_10(myDate.getSeconds());
				var nowTime = myDate.getFullYear() + '-' + add_10(myDate.getMonth()) + '-' + myDate.getDate();
				return nowTime;
			}
		},
		onNavigationBarButtonTap(e) {
			if (e.index == 0) {
				this.floatingMenuShow = !this.floatingMenuShow;
			}
		}
	};
</script>

<style scoped="true" lang="scss">
	.bumen {
		display: flex;
		justify-content: space-around;
		padding: 15upx 50upx 0;

		.uni-input {
			background: transparent;
			padding: 0;
		}
	}

	.today {
		color: #17a8f8;
		border-bottom: 5upx solid #17a8f8;
		padding: 10upx 20upx;
		font-size: 30upx;
		margin: 0 20upx 15upx;
	}

	.row {
		display: flex;
		border: 1upx solid #f2f2f2;
		align-items: center;
		background: #FFFFFF;
		padding: 10upx;
	}

	.lefttou {
		width: 100%;
		display: flex;
		align-items: center;
	}

	.tou {
		width: 100upx;
		height: 100upx;
		margin-right: 10upx;
		border-radius: 50%;
		overflow: hiideen;
	}

	.w50 {
		width: 50%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 50upx;
	}

	.num {
		border-radius: 16upx;
		background: #fb5656;
		color: #FFFFFF;
		display: inline-block;
		min-width: 32upx;
		min-height: 32upx;
		line-height: 1.5;
		padding: 0 5rpx;
		text-align: center;

	}

	.p2010 {
		padding: 20rpx 10rpx
	}

	.floating-menus {
		position: fixed;
		top: 0;
		right: 20upx;
		z-index: 10;
		opacity: 0;
		transition: all 0.4s;
		height: 0;
		overflow: hidden;
	}

	.floating-menus {
		&.show {
			opacity: 1;
			height: auto;
		}

		&.hidden {
			opacity: 0;
			height: 0;
		}
	}
	.more{
		text-align: center;
		padding-bottom: 40upx;
	}
</style>
